package com.hx.myapp.recyclerview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hx.myapp.R;

public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.GridViewHolder> {
   private Context mContext;
   private OnItemClickListener mlistener;
    public StaggeredGridAdapter(Context context , OnItemClickListener listener) {
            this.mContext = context;
            this.mlistener = listener;
    }


    @Override
    public StaggeredGridAdapter.GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new GridViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_staggered_grid_recycleview_item,parent,false)) ;
    }

    @Override
    public void onBindViewHolder(StaggeredGridAdapter.GridViewHolder holder, int position) {
        if (position % 2 != 0){
            holder.imageView.setImageResource(R.drawable.image1);
        } else{
            holder.imageView.setImageResource(R.drawable.image2);
        }

           holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(mContext,"click..." +position,Toast.LENGTH_SHORT).show();
                mlistener.onClick(position);
            }
        });
    }
    @Override
    public int getItemCount() {
        return 30;
    }
    class GridViewHolder extends RecyclerView.ViewHolder {
       private ImageView imageView;
        public GridViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv);
        }
    }
    public interface OnItemClickListener{
        void onClick(int pos);
    }
}
